package com.tinyideatech.admin.controller.car;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tinyideatech.common.core.controller.BaseController;
import com.tinyideatech.common.core.entity.ApiResult;
import com.tinyideatech.common.core.entity.PageResult;
import com.tinyideatech.common.core.text.Convert;
import com.tinyideatech.common.json.JSONObject;
import com.tinyideatech.common.utils.StringUtils;
import com.tinyideatech.framework.utils.ExcelUtils;
import com.tinyideatech.system.entity.CarDevice;
import com.tinyideatech.system.service.ICarDeviceService;
import com.tinyideatech.system.service.ICarOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @author zhangy-5
 * @date 2019-09-09
 */
@Api("设备接口")
@Controller
@RequestMapping("/car/device")
public class CarDeviceController extends BaseController {

    @Autowired
    private ICarDeviceService carDeviceService;

    @Autowired
    private ICarOrderService carOrderService;

    @GetMapping("/test")
    @ResponseBody
    public ApiResult test() {
        return ApiResult.success("Test information");
    }

    @GetMapping()
    public String list() {
        return "car/device/list";
    }

    @GetMapping("/add")
    public String add() {
        return "car/device/add";
    }

    @GetMapping("/edit/{id}")
    public String carDeviceEdit(
            @PathVariable("id") Long id,
            ModelMap map
    ) {
        CarDevice device = carDeviceService.getById(id);
        map.addAttribute("device", device);
        return "car/device/edit";
    }

    @ResponseBody
    @PostMapping("/list")
    public PageResult list(CarDevice carDevice) {
        QueryWrapper<CarDevice> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(carDevice.getDeviceId())) {
            qw.like("device_id", carDevice.getDeviceId());
        }
        IPage<CarDevice> page = startPage();
        IPage<CarDevice> carDeviceList = carDeviceService.page(page, qw);
        return page(carDeviceList);
    }

    @ResponseBody
    @PostMapping("/save")
    public ApiResult save(
            CarDevice sysRole
    ) {
        carDeviceService.saveOrUpdate(sysRole);
        return success();
    }

    @ResponseBody
    @PostMapping("/remove")
    public ApiResult remove(
            String ids
    ) {
        Long[] carDeviceIds = Convert.toLongArray(ids);
        carDeviceService.removeByIds(Arrays.asList(carDeviceIds));
        return success();
    }


    @ApiOperation("激活码和设备绑定")
    @PostMapping("/get")
    @ResponseBody
    public ApiResult get() {
        return ApiResult.success("yeah,gotten you in");
    }


    @ApiOperation("激活码和设备绑定")
    @PostMapping("/bind/code")
    @ResponseBody
    public ApiResult DeviceBindCode(@RequestParam String deviceId,
                                    @RequestParam String code) {
        boolean dcResult = carDeviceService.bindCode2Device(deviceId, code);
        return ApiResult.success(dcResult);
    }

    @ApiOperation("手机号和设备绑定")
    @PostMapping("/bind/mobile")
    @ResponseBody
    public ApiResult DeviceBindMobile(@RequestParam String deviceId,
                                      @RequestParam String mobile,
                                      @RequestParam String pwd,
                                      @RequestParam String code) throws Exception {
        int dcResult = carDeviceService.bindCode2Mobile(deviceId, mobile, code, pwd);
        if (dcResult == 0) {
            return ApiResult.success("Bind Success");
        } else if (dcResult == 301) {
            return ApiResult.warn("Duplicate Binding");
        } else if (dcResult == 302) {
            return ApiResult.warn("Code Invalid");
        }
        return ApiResult.error("Internal Error");
    }


    @ApiOperation("设备验证激活码")
    @PostMapping("/verify/code")
    @ResponseBody
    public ApiResult verifyCode(@RequestParam String code) {
        JSONObject jsonObject = new JSONObject();
        if (!(code.equals(""))) {
            jsonObject.put("status", "active");
            jsonObject.put("feature", carDeviceService.verifyCode(code));
        }
        return ApiResult.success(jsonObject);
    }

    /**
     * Excel下载
     *
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "导出订单Excel")
    @GetMapping("/export/order")
    public void orderExcel(HttpServletResponse response) {
        XSSFWorkbook wb = carOrderService.downloadRecorder();
        String fileName = "CarOrder报表.xlsx";
        ExcelUtils.init(fileName, response, wb);
    }
}
